package com.example.mychat.Dao;

import com.example.mychat.Pojo.chatInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface wsDao {
    @Insert("insert into chat_for_person(chatdata, chatid) values (#{mes},#{chat_id})")
    void MesInsert_person(String mes, String chat_id);

    //这一步应该在加好友操作时执行
    @Insert("insert into chatdata_map(chat_id, memberId,chatname) values (#{chatid},#{member},#{chatname})")
    int mapInsert(String chatid, String member,String chatname);

    @Update("update chat_for_person set chatdata=concat(chatdata,',',#{mes}) where chatid=#{chat_id}")
    void MesUpdate_person(String mes, @Param("chat_id") String chat_id);

    //    @Select("select chat_id from chatdata_map where memberId between #{toId} and #{sendId}")
    @Select("select chat_id from chatdata_map where memberId like concat('%',#{toId},'%') and memberId like concat('%',#{sendId},'%')")
    String chatIdSelect(String toId, String sendId);

    /**
     * 使用if条件判断提取出memberId字段中想要拿到的那部分id
     * @param my_Id
     * @return
     */
    @Select("select chatname,chatid,chatdata, (IF(LOCATE(#{my_Id}, memberId) = 1, SUBSTRING_INDEX(memberId, ',', -1), SUBSTRING_INDEX(memberId, ',', 1))) as userid from chatdata_map,chat_for_person where memberId like concat('%',#{my_Id},'%') and chatdata_map.chat_id=chat_for_person.chatid")
    @Results(id = "chatInfoResultMap", value = {
            @Result(property = "chatid", column = "chatid"),
            @Result(property = "userid", column = "userid"),
            @Result(property = "chatname", column = "chatname"),
            @Result(property = "chatdata", column = "chatdata", typeHandler = chatInfo.ChatDataArrayTypeHandler.class)
    })
    List<chatInfo> selectdata(@Param("my_Id") String my_Id);

    @Select("select chat_id from chatdata_map where chat_id=#{chatid}")
    String selectChatId(String chatid);
}